import sys
import re
import random

sys.path.append('./')
sys.path.append('/Users/xuchaosheng/anaconda3/lib/python3.6/site-packages')

from config import *

client = MongoClient('127.0.0.1', 27017, connect = False)

industryKeyword_db = client['industryKeyword_db']
industryKeyword_coll = industryKeyword_db['industryKeyword_coll']


@app.route('/trainPlatform/industryKeyword/search', methods = ['GET'])
def industryKeyword_search():
    data = []

    for item in industryKeyword_coll.find():
        data.append({
            'name': item['name'],
            'keywords': item['keywords']
        })

    resp = {
        'success': True,
        'data': data
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/industryKeyword/add', methods = ['GET'])
def industryKeyword_add():
    name = request.args.get('name', '').strip()
    kw = request.args.get('kw', '').strip()

    if not name or not kw:
        resp = {
            'success': False,
            'msg': '行业名称与关键字缺一不可'
        }

        response = make_response(jsonify(resp))
        return response

    if not industryKeyword_coll.find_one({'name': name}):
        resp = {
            'success': False,
            'msg': name + '领域尚不存在，请先新建改领域'
        }

        response = make_response(jsonify(resp))
        return response

    item = industryKeyword_coll.find_one({'name': name})

    for i in kw.split('/'):
        if i in item['keywords']:
            continue

        industryKeyword_coll.update_one({'name': name}, {'$push': {
            'keywords': i.strip()
        }})

    resp = {
        'success': True,
        'msg': '成功导入'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/industryKeyword/del', methods = ['GET'])
def industryKeyword_del():
    name = request.args.get('name', '').strip()
    kw = request.args.get('kw', '').strip()

    if not name or not kw:
        resp = {
            'success': False,
            'msg': '行业名称与关键字缺一不可'
        }

        response = make_response(jsonify(resp))
        return response

    if not industryKeyword_coll.find_one({'name': name}):
        resp = {
            'success': False,
            'msg': name + '领域尚不存在，请先新建改领域'
        }

        response = make_response(jsonify(resp))
        return response

    industryKeyword_coll.update_one({'name': name}, {'$pull': {
        'keywords': kw
    }})

    resp = {
        'success': True,
        'msg': '成功删除'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/industryKeyword/newIndustry', methods = ['GET'])
def industryKeyword_newIndustry():
    name = request.args.get('name', '').strip()

    if not name:
        resp = {
            'success': False,
            'msg': '行业名称不可为空'
        }

        response = make_response(jsonify(resp))
        return response

    industryKeyword_coll.insert_one({'name': name, 'keywords': []})

    resp = {
        'success': True,
        'msg': '成功新建一个行业'
    }

    response = make_response(jsonify(resp))
    return response
